package test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			FileInputStream fstream = new FileInputStream("C://OrdenCompraCC.xml");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			String xml = "";
			//Read File Line By Line
			while ((strLine = br.readLine()) != null)   {
			// Print the content on the console
				xml+=strLine;
			}
			  //Close the input stream
			  in.close();
			  
			  
			  Hashtable props = new Hashtable();

		        props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		        props.put(InitialContext.PROVIDER_URL, "jnp://localhost:1099");
		        
		        InitialContext ctx = new InitialContext(props);

                // buscar la Connection Factory en JNDI
                QueueConnectionFactory qfactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");

                // buscar la Cola en JNDI
                Queue queue = (Queue) ctx.lookup("queue/ordenCompraQueue");

                // crear la connection y la session a partir de la connection
                QueueConnection qCon = qfactory.createQueueConnection();
                QueueSession qSession = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

                // crear un producer para enviar mensajes usando la session
                QueueSender qSender = qSession.createSender(queue);
                
                TextMessage message = qSession.createTextMessage();
                message.setText(xml);

                // enviar el mensaje
                qSender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
